package com.cmc.cloud.cmclink.doc.controller.admin;

import com.cmc.cloud.cmclink.common.properties.CmclinkCommonProperties;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.cos.core.dto.CosUploadResponse;
import com.cmes.framework.cos.core.service.CosClientV1;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author 周贤
 * @date 2025/3/18
 */
@Tag(name = "文件上传下载等")
@RestController
@RequestMapping(UrlConstants.APPLICATION_URL + "/file")
@Validated
public class FileUploadController {
    @Resource
    private CosClientV1 cosClientV1;

    @Resource
    private CmclinkCommonProperties cmclinkProperties;

    @Operation(summary = "上传模板文件")
    @PostMapping(value = "/upload")
    public CommonResult<CosUploadResponse> uploadFile(@RequestPart @RequestParam(value = "file") MultipartFile file) throws IOException {
        CommonResult<CosUploadResponse> commonResult = cosClientV1.uploadFile(file, cmclinkProperties.getCosBucketName(), true);
        return commonResult;
    }


    @Operation(summary = "预览")
    @GetMapping ("/previewFile")
    public CommonResult<String>  preview (@RequestParam(required = true)String resourceId,@RequestParam(required = true) String resourceName,@RequestParam(required = true) Boolean isInner) throws IOException {
        CommonResult<String> stringCommonResult = cosClientV1.previewFile(resourceId, cmclinkProperties.getCosBucketName(), resourceName, isInner);
        String data = stringCommonResult.getData();
        return CommonResult.success(data);
    }
}
